package com.youlai.boot.modules.delivery.controller;

import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.modules.delivery.model.DeliveryForm;
import com.youlai.boot.modules.delivery.model.DeliveryPageQuery;
import com.youlai.boot.modules.delivery.model.DeliveryVO;
import com.youlai.boot.modules.delivery.service.DeliveryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@Tag(name = "物流公司接口")
@RestController
@RequestMapping("/api/v1/delivery")
@RequiredArgsConstructor
public class DeliveryController {

    private final DeliveryService deliveryService;

    @Operation(summary = "物流公司分页列表")
    @GetMapping("/page")
    public PageResult<DeliveryVO> getDeliveryPage(DeliveryPageQuery query) {
        Page<DeliveryVO> result = deliveryService.getDeliveryPage(query);
        return PageResult.success(result);
    }

    @Operation(summary = "物流公司详情")
    @GetMapping("/{id}")
    public Result<DeliveryVO> getDelivery(@PathVariable Long id) {
        DeliveryVO delivery = deliveryService.getDeliveryById(id);
        return Result.success(delivery);
    }

    @Operation(summary = "添加物流公司")
    @PostMapping
    public Result<Long> addDelivery(@Valid @RequestBody DeliveryForm delivery) {
        Long id = deliveryService.saveDelivery(delivery);
        return Result.success(id);
    }

    @Operation(summary = "修改物流公司")
    @PutMapping("/{id}")
    public Result<Boolean> updateDelivery(
            @PathVariable Long id,
            @Valid @RequestBody DeliveryForm delivery) {
        delivery.setId(id);
        deliveryService.updateDelivery(delivery);
        return Result.success(true);
    }

    @Operation(summary = "删除物流公司")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteDelivery(@PathVariable Long id) {
        boolean result = deliveryService.deleteDelivery(id);
        return Result.judge(result);
    }

    @Operation(summary = "获取商品物流公司列表")
    @GetMapping("/product/{productId}/page")
    public PageResult<DeliveryVO> getProductDeliveryList(@PathVariable Long productId, DeliveryPageQuery query) {
        Page<DeliveryVO> deliveryList = deliveryService.getProductDeliveryPage(productId, query);
        return PageResult.success(deliveryList);
    }

    @Operation(summary = "添加商品物流公司")
    @PostMapping("/product/{productId}/delivery/{deliveryId}")
    public Result<Boolean> addProductDelivery(@PathVariable Long productId, @PathVariable Long deliveryId) {
        boolean result = deliveryService.saveProductDelivery(productId, deliveryId);
        return Result.judge(result);
    }

    @Operation(summary = "删除商品物流公司")
    @DeleteMapping("/product/{productId}/delivery/{deliveryId}")
    public Result<Boolean> deleteProductDelivery(@PathVariable Long productId, @PathVariable Long deliveryId) {
        boolean result = deliveryService.deleteProductDelivery(productId, deliveryId);
        return Result.judge(result);
    }
}